package com.demo.springboot;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

@SpringBootConfiguration
public class UserConfig {

    /**
     * 表示属性等于某个值时才装配
     * matchIfMissing : 配置文件中没有那个属性的默认值
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "runnable.enabled",havingValue = "true",matchIfMissing = true)
    public Runnable createPropertyRunnable(){
        return ()->{};
    }

    /***
     * 表示存在某个Bean时才装配
     * ConditionalOnBean           存在
     * ConditionalOnMissingBean    不存在
     * @return
     */
    @Bean
    @ConditionalOnBean(name = "user")
    public Runnable createBeanRunnable(){
        return ()->{};
    }

    /***
     * 表示classpath中存在某个Class时才装配
     * ConditionalOnClass       存在
     * ConditionalOnMissClass   不存在
     * @return
     */
    @Bean
    @ConditionalOnClass(name = "com.demo.springboot.User")
    public Runnable createClassRunnable(){
        return ()->{};
    }
}
